package com.castle.fortress.admin.cms.controller;

import com.castle.fortress.admin.cms.dto.CmsColConfigDto;
import com.castle.fortress.admin.cms.dto.CmsFormDataDto;
import com.castle.fortress.admin.cms.dto.CmsModelDto;
import com.castle.fortress.admin.cms.entity.CmsModelEntity;
import com.castle.fortress.admin.cms.service.CmsColConfigService;
import com.castle.fortress.admin.cms.service.CmsModelDataService;
import com.castle.fortress.admin.cms.service.CmsModelService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * cms模型数据表 控制器
 *
 * @author castle
 * @since 2022-03-02
 */
@Api(tags="cms模型数据表管理控制器")
@Controller
public class CmsModelDataController {
    @Autowired
    private CmsModelService cmsModelService;
    @Autowired
    private CmsColConfigService cmsColConfigService;
    @Autowired
    private CmsModelDataService cmsModelDataService;

    /**
     * cms模型数据表单信息
     * @param cmsModelDto
     * @return
     */
    @ApiOperation("cms模型数据表单信息")
    @GetMapping("/cms/modelData/formInfo")
    @ResponseBody
    @RequiresPermissions("cms:modelData:formInfo")
    public RespBody<CmsModelDto> formInfo(CmsModelDto cmsModelDto){
        if(cmsModelDto == null || cmsModelDto.getId() == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsModelEntity modelEntity = cmsModelService.getById(cmsModelDto.getId());
        CmsColConfigDto cmsColConfigDto = new CmsColConfigDto();
        cmsColConfigDto.setModelId(cmsModelDto.getId());
        List<CmsColConfigDto> cols = cmsColConfigService.listCmsColConfig(cmsColConfigDto);
        CmsModelDto dto = ConvertUtil.transformObj(modelEntity,CmsModelDto.class);
        dto.setCols(cols);
        return RespBody.data(dto);
    }

    /**
     * 数据沉淀
     * @param formDataDto 表模型
     * @return
     */
    @ApiOperation("沉淀数据")
    @PostMapping("/cms/modelData/saveData")
    @ResponseBody
    public RespBody<String> saveData(@RequestBody CmsFormDataDto formDataDto){
        if(CommonUtil.verifyParamNull(formDataDto,formDataDto.getModelId(),formDataDto.getFormData())){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        cmsModelDataService.saveData(formDataDto.getModelId(),formDataDto.getFormData());
        return RespBody.data("保存成功");
    }


}
